package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationOptionService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.util.PageBean;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("specification")
public class SpecificationController {
    private Logger logger = LoggerFactory.getLogger(BrandController.class);
    @Reference
    private SpecificationService specificationService;
    @Reference
    private SpecificationOptionService specificationOptionService;

    @RequestMapping("toSpecification")
    public String specification(){
        return "/admin/specification";
    }

    @RequestMapping("getinfo")
    @ResponseBody
    public dataResult getinfo(@RequestParam(defaultValue = "1")Integer number,@RequestParam(defaultValue = "3")Integer pageSize,String sname){
        try {
            PageBean<TbSpecification> b = specificationService.getinfo(number,pageSize,sname);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),b);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("addinfo")
    @ResponseBody
    public dataResult addinfo(String specName,String OptionString){

        List<TbSpecificationOption> list = JSON.parseArray(OptionString, TbSpecificationOption.class);
        try {
            Integer i = specificationService.addinfo(specName,list);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("增加失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public dataResult deleteAll(@RequestParam("ids[]")Long[] ids){
        if (ids == null){
            return new dataResult(ResponseStatus.BAD_REQUEST.getIndex(), ResponseStatus.BAD_REQUEST.getName());
        }
        try {
            Integer i = specificationService.deleteAll(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("删除失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public dataResult toUpdate(Long id){
        if (id == null){
            return new dataResult(ResponseStatus.BAD_REQUEST.getIndex(), ResponseStatus.BAD_REQUEST.getName());
        }
        try {
            TbSpecification t = specificationService.toUpdate(id);
            List<TbSpecificationOption> list = specificationOptionService.getBySid(id);
            Map<String,Object> map = new HashMap<>();
            map.put("t",t);
            map.put("list",list);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),map);
        }catch (Exception e){
            logger.error("删除失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("updateinfo")
    @ResponseBody
    public dataResult  updateinfo(String specName,String OptionString,Long id){
        List<TbSpecificationOption> list = JSON.parseArray(OptionString, TbSpecificationOption.class);
        try {
            Integer i = specificationService.updateinfo(specName,list,id);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("修改失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }
}
